package controllers;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import dao.RoomDAORemote;
import dto.RoomDTO;

@ManagedBean
@SessionScoped
public class RoomController implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@EJB
	RoomDAORemote roomEjb;

	@ManagedProperty(value = "#{roomDTO}")
	RoomDTO roomDTO;

	private List<String> checkbox = new ArrayList<String>();
	int error = 0;

	List<RoomDTO> roomList = new ArrayList<RoomDTO>();
	private String messageClass;

	@PostConstruct
	public void createList() {
		roomList = roomEjb.getAllRooms();
	}

	public void setRoomDTO(RoomDTO roomDTO) {
		this.roomDTO = roomDTO;
	}

	public RoomDTO getRoomDTO() {
		return roomDTO;
	}

	public void addRoom(ActionEvent actionEvent) {
		
		error = 0;
		FacesContext context = FacesContext.getCurrentInstance();		

		if (roomEjb.getRoomByAddress(roomDTO.getAddress()) != null) {
			context.addMessage(actionEvent.getComponent().getClientId(),
					new FacesMessage("Room already exists!"));
			messageClass = "errorMessage";
			error = 1;
		}

		if (roomDTO.getAddress().equals("")) {
			context.addMessage(actionEvent.getComponent().getClientId(),
					new FacesMessage("Please insert an address!"));
			messageClass = "errorMessage";
			error = 1;
		}

		if (roomDTO.getNumberOfStations() <= 0) {
			context.addMessage(actionEvent.getComponent().getClientId(),
					new FacesMessage(
							"Number of stations must be greater than 0!"));
			messageClass = "errorMessage";
			error = 1;
		}
		if (error == 0) {
			roomEjb.addRoom(roomDTO);
			roomDTO.setAddress("");
			roomDTO.setNumberOfStations(0);
			context.addMessage(actionEvent.getComponent().getClientId(),
					new FacesMessage("Room succesfully added!"));
			messageClass = "success";
		}

		setRoomList(roomEjb.getAllRooms());
		
	}

	public void deleteRoom() {
		for (String r : checkbox)
			roomEjb.deleteRoom(roomEjb.getRoomById(Integer.parseInt(r)));
		checkbox.clear();
		setRoomList(roomEjb.getAllRooms());
	}

	public void updateRooms(ActionEvent actionEvent) {
		FacesContext context = FacesContext.getCurrentInstance();
		int ok = roomList.size();
		for (RoomDTO r : roomList) {
			if (r.getNumberOfStations() <= 0) {
				context.addMessage(actionEvent.getComponent().getClientId(),
						new FacesMessage(
								"Number of stations must be greater than 0!"));
				messageClass = "errorMessage";
			} else {
				ok--;
				roomEjb.updateRoom(r, r);

			}
		}
		if (ok == 0) {
			context.addMessage(actionEvent.getComponent().getClientId(),
					new FacesMessage("All rooms were successfully updated!"));
			messageClass = "success";
		} else {
			context.addMessage(actionEvent.getComponent().getClientId(),
					new FacesMessage("Update not completed!"));
			messageClass = "errorMessage";
		}
		setRoomList(roomEjb.getAllRooms());
	}

	public void setRoomList(List<RoomDTO> roomList) {
		this.roomList = roomList;
	}

	public List<RoomDTO> getRoomList() {
		return roomList;
	}

	public void setCheckbox(List<String> checkbox) {
		this.checkbox.addAll(checkbox);
	}

	public List<String> getCheckbox() {
		return checkbox;
	}

	public void setMessageClass(String messageClass) {
		this.messageClass = messageClass;
	}

	public String getMessageClass() {
		return messageClass;
	}
}
